package hotLeetcode;

import java.sql.ResultSet;
import java.util.Arrays;

public class H16Leetcode189 {
    public void rotate(int[] nums, int k) {
        if (k == 0 || nums.length == 1) return;
        int len = nums.length;

        k = k % len;
        int right = len - k;


        int[] temp = new int[len - k];
        for (int i = 0; i < right; i++) {
            temp[i] = nums[i];
        }

        for (int i = 0,j = right; i < k; i++ , j++) {
            nums[i] = nums[j];

        }

        for (int i = k  , j = 0; i < nums.length; i++ , j ++) {
            nums[i] = temp[j];

        }


    }

    //解法 2  翻转整个数组，然后逐个击破
    public void rotate2(int[] nums, int k) {
        //对于 数组长度小于 k 的可以进行 mod 处理，
        int len = nums.length;
        k = k % len;
        reverse(nums, 0, len - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, len - 1);





    }


    private void reverse(int[] nums, int i, int j) {
        while (i < j) {
            int temp = nums[j];
            nums[j--] = nums[i];
            nums[i++] = temp;

        }
    }




    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        H16Leetcode189 h16Leetcode189 = new H16Leetcode189();
        h16Leetcode189.rotate(nums,3);
        System.out.println(Arrays.toString(nums));
    }
}
